package com.hzp.lolnews.activity.view;


import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hzp.lolnews.R;
import com.hzp.lolnews.activity.presenter.LoginPresenter;
import com.hzp.lolnews.base.BaseActivity;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class LoginActivity extends BaseActivity implements View.OnClickListener, ILoginView {


    private EditText edName;
    private EditText edPassword;
    private LoginPresenter presenter;
    private UMShareAPI mShareAPI;
    private ImageView login_back;

    @Override
    protected void initView() {
        //初始化
        mShareAPI = UMShareAPI.get(LoginActivity.this);
        presenter = new LoginPresenter(this, this);
        edName = (EditText) findViewById(R.id.login_input_phone);
        edPassword = (EditText) findViewById(R.id.login_input_pwd);
        Button btnLogin = (Button) findViewById(R.id.login_sign);
        login_back = (ImageView) findViewById(R.id.login_back);
        login_back.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        Button btnSign = (Button) findViewById(R.id.login);
        btnSign.setOnClickListener(this);
        Button btnQQ = (Button) findViewById(R.id.login_qq);
        btnQQ.setOnClickListener(this);
        Button btnSina = (Button) findViewById(R.id.login_wb);
        btnSina.setOnClickListener(this);
        Button btnWechat = (Button) findViewById(R.id.login_wx);
        btnWechat.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    //接口回掉结果
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//         Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.login:
                presenter.login();
                break;
            case R.id.login_sign:
                presenter.register();
                break;
            case R.id.login_qq:
                //进行授权验证  QQ
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                //获取用户信息
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                break;
            case R.id.login_wb:
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                //获取用户信息
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                finish();
                break;
            case R.id.login_wx:
                //微信
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.WEIXIN, umAuthListener);
                //获取用户信息
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, umAuthListener);
                break;
            case R.id.login_back:
                finish();

        }

    }

    @Override
    public String getUserName() {
        return edName.getText().toString();
    }

    @Override
    public String getPassWord() {
        return edPassword.getText().toString();
    }

    @Override
    public void register() {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    public void toSucceedActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showLoginFailedView() {
        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
    }
}
